package com.yonyou.framework.library.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yonyou.framework.library.R;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 作者：joseph_l on 11/12/15 15:01
 * 邮箱：ljt@yonyou.com
 */
public class CommonUtils {

    private static Toast toast;

    /**
     * 字符串为空判断
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0 || str.equalsIgnoreCase("null")
                || str.isEmpty() || str.equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 显示toast
     *
     * @param context
     * @param text
     * @param isLongToast
     */
    public static void makeEventToast(Context context, String text,
                                      boolean isLongToast) {

        if (null == context || null == text)
            return;

        View v = LayoutInflater.from(context).inflate(R.layout.toast_view,
                null);
        TextView textView = (TextView) v.findViewById(R.id.text);
        textView.setText(text);

        //每次创建Toast时先做一下判断
        //如果前面已经有Toast在显示，则只是更新toast内容，而不再创建，提升用户体验
        if (toast != null) {
            textView.setText(text);
        } else {
            toast = Toast.makeText(context, text, isLongToast ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
        }

        toast.setView(v);
        toast.show();
    }

    /**
     * 显示toast
     *
     * @param context
     * @param id
     * @param isLongToast
     */
    public static void makeEventToast(Context context, int id,
                                      boolean isLongToast) {

        if (null == context)
            return;

        String text = context.getResources().getString(id);

        View v = LayoutInflater.from(context).inflate(R.layout.toast_view,
                null);
        TextView textView = (TextView) v.findViewById(R.id.text);
        textView.setText(text);

        if (toast != null) {
            textView.setText(text);
        } else {
            toast = Toast.makeText(context, text, isLongToast ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
        }

        toast.setView(v);
        toast.show();

    }

    /**
     * 调用拨打电话
     *
     * @param context 上下文
     * @param number  电话号码
     */
    public static void callPhone(Context context, String number) {
        if (null != number) {
            try {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse(String.format("tel:%s", number)));
                context.startActivity(intent);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else {
            CommonUtils.makeEventToast(context, "电话号码不存在！", false);
        }
    }


    /**
     * 获取Mac地址
     *
     * @return
     */
    public static String getLocalIpAddress(Context context) {
        try {
            Enumeration en = NetworkInterface.getNetworkInterfaces();
            for (; en.hasMoreElements(); ) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }

        return null;
    }

    public static String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer(b.length);
        String stmp = "";
        int len = b.length;
        for (int n = 0; n < len; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            if (stmp.length() == 1) {
                hs = hs.append("0").append(stmp);
            } else {
                hs = hs.append(stmp);
            }
        }

        return String.valueOf(hs);
    }

    /**
     * 获取MAC
     *
     * @param context
     * @return
     */
    public static String getLocalMacAddressFromIp(Context context) {
        String mac_s = "";
        try {
            byte[] mac;
            NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress(context)));
            mac = ne.getHardwareAddress();
            mac_s = byte2hex(mac);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mac_s;
    }

    public static void showSoftInput(Activity activity, final EditText editText) {
        View foucsView = activity.getWindow().getCurrentFocus();
        if (null != foucsView) {
            foucsView.clearFocus();
        }
        editText.requestFocus();
        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(editText, 0);
    }

}
